﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace game
{
    public class PointLight
    {
        public Vector4 Ambient { get; set; }
        public Vector4 Diffuse
        {
            get { return diff; }
            set { diff = value; }
        }
        Vector4 diff;
        public Vector4 Specular { get; set; }
        public Vector3 Position { get; set; }
        public Vector3 Attenuation012
        {
            get { return curAttenuation; }
            set { origAttenuation = value; }
        }

        public bool IsStatic { get; protected set; }
        public bool IsAlive { get; protected set; }

        public int LifeTimeMs { get; set; }
        public float GrowSpeed { get; set; }
        public float CollapseSpeed { get; set; }

        protected Vector3 origAttenuation;
        protected Vector3 curAttenuation;

        protected int curLifeTimeMs = 0;
        protected float intensityState = 1.0f;

        protected bool growing = false;
        protected bool collapsing = false;

        public PointLight(bool isStatic, Vector3 position)
        {
            Position = position;
            IsStatic = isStatic;
            Attenuation012 = new Vector3(0, 0.4f, 0.05f);
            Diffuse = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
            if (!IsStatic)
            {
                IsAlive = true;
                GrowSpeed = 0.01f;
                CollapseSpeed = 0.01f;
                LifeTimeMs = 3000;
            }
        }

        #region if IsStatic = false

        public void Start()
        {
            curLifeTimeMs = 0;
            intensityState = 0.0f;
            IsAlive = true;
            growing = true;
            collapsing = false;
        }

        public void ShortenLifeBy(int millis)
        { LifeTimeMs -= millis; }

        #endregion

        #region if IsStatic = true

        public void TurnOn() { IsAlive = true; }
        public void TurnOff() { IsAlive = false; }

        #endregion

        public void Update(GameTime gameTime)
        {
            if (!IsStatic)
            {
                if (growing)
                {
                    if (intensityState >= 1.0f)
                    {
                        intensityState = 1.0f;
                        growing = false;
                    }
                    intensityState += GrowSpeed * gameTime.ElapsedGameTime.Milliseconds;
                }
                else if (collapsing)
                {
                    if (intensityState <= 0.0f)
                    {
                        intensityState = 0.0f;
                        collapsing = false;
                        IsAlive = false;
                    }
                    intensityState -= CollapseSpeed * gameTime.ElapsedGameTime.Milliseconds;
                }
                else
                {
                    if (curLifeTimeMs >= LifeTimeMs)
                        collapsing = true;
                    curLifeTimeMs += gameTime.ElapsedGameTime.Milliseconds;
                }
                Vector3.Divide(ref origAttenuation, intensityState, out curAttenuation);
            }
            else
            {
                curAttenuation = origAttenuation;
            }
        }


    }
}
